//
//  BaseCollectionView.m
//  GoldBook
//
//  Created by wmz on 2021/9/4.
//  Copyright © 2021 wmz. All rights reserved.
//

#import "BaseCollectionView.h"
#import "BaseCollectionViewCell.h"
@implementation BaseCollectionView

- (NSInteger)pageNum{
    if(!_pageNum){
        _pageNum = 1;
    }
    return _pageNum;
}

- (void)setNeedRefreshHeader:(BOOL)needRefreshHeader{
    _needRefreshHeader = needRefreshHeader;
    if (needRefreshHeader) {
        @HDWeakify(self)
        self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @HDStrongify(self)
            self.pageNum = 1;
            if (self.requestNewDataHandler) self.requestNewDataHandler();
        }];
    }else{
        self.mj_header = nil;
    }
}

- (void)setNeedRefreshFooter:(BOOL)needRefreshFooter{
    _needRefreshFooter = needRefreshFooter;
    if (needRefreshFooter) {
        @HDWeakify(self)
        self.mj_footer = [MJRefreshAutoNormalFooter  footerWithRefreshingBlock:^{
            @HDStrongify(self)
            if (!self.pageNum) self.pageNum = 1;
            self.pageNum += 1;
            if (self.requestMoreDataHandler) self.requestMoreDataHandler();
        }];
    }else{
        self.mj_footer = nil;
    }
}

- (void)reloadData:(BOOL)isNoMore{
    [self reloadData];
    if (self.pageNum == 1) {
        if (self.needRefreshFooter) {
            [self.mj_footer resetNoMoreData];
            if (isNoMore) {
                [self.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.mj_footer endRefreshing];
            }
        }
        if (self.needRefreshHeader && self.mj_header.isRefreshing) [self.mj_header endRefreshing];
    }else{
        if (self.needRefreshFooter) {
            if (isNoMore && self.mj_footer.isRefreshing) {
                [self.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.mj_footer endRefreshing];
            }
        }
    }
    [self showOrHideNoDataPlacholderView];
}

- (void)reloadFail{
    if (self.needRefreshHeader && self.mj_header.isRefreshing) [self.mj_header endRefreshing];
    if (self.needRefreshFooter && self.mj_footer.isRefreshing) [self.mj_footer endRefreshing];
    if (self.pageNum == 1) {
        [self reloadData];
    }
    [self showOrHideNetworkErrorPlacholderView];
}

- (void)showOrHideNoDataPlacholderView {
    if (self.needShowNoDataView && !self.hd_hasData) {
        self.mj_header.hidden = YES;
        self.mj_footer.hidden = YES;
        self.placeholderViewModel.image = @"no_data";
        self.placeholderViewModel.title = @"暂无数据";
        [self hd_showPlaceholderViewWithModel:self.placeholderViewModel];
    } else {
        self.mj_header.hidden = NO;
        self.mj_footer.hidden = NO;
        [self hd_removePlaceholderView];
    }
}

- (void)showOrHideNetworkErrorPlacholderView {
    if (self.needShowErrorView && !self.hd_hasData) {
        self.mj_header.hidden = YES;
        self.mj_footer.hidden = YES;
        self.placeholderViewModel.image = @"fail_b";
        self.placeholderViewModel.title = @"网络开小差啦";
        [self hd_showPlaceholderViewWithModel:self.placeholderViewModel];
    } else {
        [self hd_removePlaceholderView];
        self.mj_header.hidden = NO;
        self.mj_footer.hidden = NO;
    }
}

- (UIViewPlaceholderViewModel *)placeholderViewModel{
    if (!_placeholderViewModel) {
        UIViewPlaceholderViewModel *placeholderViewModel = UIViewPlaceholderViewModel.new;
        placeholderViewModel.title = @"暂无数据";
        placeholderViewModel.image = @"no_data";
        placeholderViewModel.needRefreshBtn = self.needRefreshBTN;
        placeholderViewModel.refreshBtnTitle = @"刷新";
        @HDWeakify(self)
        placeholderViewModel.clickOnRefreshButtonHandler = ^{
            @HDStrongify(self)
            if (self.tappedRefreshBtnHandler) self.tappedRefreshBtnHandler();
        };
        _placeholderViewModel = placeholderViewModel;
        
    }
    return _placeholderViewModel;
}

- (BOOL)hd_hasData {
    NSInteger totalCount = 0;
    if ([self isKindOfClass:[UITableView class]]) {
        UITableView *tableView = (UITableView *)self;
        for (NSInteger section = 0; section < tableView.numberOfSections; section++)
            totalCount += [tableView numberOfRowsInSection:section];
    } else if ([self isKindOfClass:[UICollectionView class]]) {
        UICollectionView *collectionView = (UICollectionView *)self;
        for (NSInteger section = 0; section < collectionView.numberOfSections; section++)
            totalCount += [collectionView numberOfItemsInSection:section];
    }
    return totalCount;
}

@end
